<template>
  <div class="myAddress">
    <!-- 顶部标题 -->
    <van-nav-bar :title="$t('mine.myLocation')" left-arrow @click-left="$router.go(-1)" />
    <!-- 地址列表 -->
    <van-address-list
      v-model="chosenAddressId"
      :list="addressList"
      @select="onSelect"
      @add="onAdd"
      @edit="onEdit"
    ></van-address-list>
  </div>
</template>

<script>
// 轻提示
import { Toast } from 'vant'
export default {
  data () {
    return {
      //  默认收货地址
      chosenAddressId: 0,
      // 收货地址数据
      addressList: []
    }
  },
  methods: {
    onAdd () {
      this.$router.push('/mine/myAddress/addAddress')
    },
    onEdit (item, index) {},
    onSelect (item, index) {
      let id = item.id
      let uid = localStorage.getItem('userId')
      this.$http.get(`/address/${uid}/${id}`).then(res => {
        if (res.data.ok === 1) {
          Toast('已修改默认收货地址')
        } else {
          Toast('修改失败')
          return false
        }
      })
    },
    getAddress () {
      let id = localStorage.getItem('userId')
      this.$http.get(`address/${id}`).then(res => {
        res.data.data.forEach(v => {
          v.address = v.province + v.city + v.county + v.addressDetail
          if (v.isDefault === 1) {
            this.chosenAddressId = v.id
          }
        })
        this.addressList = res.data.data
      })
    }
  },
  created () {
    this.getAddress()
    this.addressList.forEach(v => {
      if (v.isDefault === 1) {
        v.isDefault = true
        this.chosenAddressId = v.id
      }
    })
  }
}
</script>

<style lang="less">
.myAddress {
  .van-button--danger {
    background-color: #45c763;
    border-color: #45c763;
  }
  .van-radio__icon--checked .van-icon {
    color: #393835;
  }
  .van-row {
    position: fixed;
    width: 100%;
    height: 66px;
    bottom: -13px;
    z-index: 2000;
    background-color: #fff;
  }
}
</style>
